package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.ShopModel;

/**
 * Created by qi.wang on 2017/3/16.
 */

public class GoodsAdapter extends BaseAdapter{
    public List<ShopModel.ListGoodsBean> list=new ArrayList<>();
    Context context;
    LayoutInflater inflater;
    Listeners mListener=null;
    public GoodsAdapter(Context context, List<ShopModel.ListGoodsBean> data){
        this.context=context;
        this.list=data;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.list_item_goods,parent,false);
            holder.goods_name=(TextView)convertView.findViewById(R.id.goods_name);
            holder.goods_price=(TextView)convertView.findViewById(R.id.goods_price);
            holder.goods_img=(ImageView) convertView.findViewById(R.id.goods_img);
            holder.goCar=(ImageView) convertView.findViewById(R.id.goCar);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.goCar.setTag(position);
        ShopModel.ListGoodsBean newsModel=list.get(position);
        holder.goods_name.setText(newsModel.getGoods_name());
        if(newsModel.getPrice()!=null) {
            holder.goods_price.setText("¥" + newsModel.getPrice());
        }else{
            holder.goods_price.setText("¥ 0.0");
        }
        try {
            Glide.with(context).load(newsModel.getImages()).asBitmap().error(R.mipmap.sebei_logo).into(holder.goods_img);
        }catch (Exception e){
            e.printStackTrace();
        }
        holder.goCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer) v.getTag();
                ShopModel.ListGoodsBean newsModel=list.get(num);
                if(mListener!=null){
                    mListener.addCar(newsModel);
                }
            }
        });
        return convertView;
    }

    public static class ViewHolder{
        TextView goods_name,goods_price;
        ImageView goods_img,goCar;
    }
    public interface Listeners {

        public void addCar(ShopModel.ListGoodsBean newsModel);
    }
    public void setLoadingListener(Listeners listener) {
        mListener = listener;
    }
}
